package com.example.myapp;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class TabActicity extends AppCompatActivity implements View.OnClickListener{

    private final Fragment messageFragment = new MessageFragment();
//
    private final Fragment bookFragment = new BookFragment();
//
    private final Fragment mineFragment = new MineFragment();
    private FragmentManager fragmentManager;
    private LinearLayout messageLayout,bookLayout,mineLayout;


//    onCreate()：   在活动创建时调用stop
//    onStart()：    在活动为用户可见时调用
//    onResume()：   在应用程序与用户开始交互时被调用
//    onPause()：    活动暂停无法与用户交互时调用
//    onStop()：     活动不可见时调用
//    onDestory()：  活动被系统销毁之前调用
//    onRestart()：  当活动在destory前stop后重新打开时调用

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  // 打开页面activity_main.xml
        // 初始化控件
        messageLayout = findViewById(R.id.messageLayout);
        bookLayout = findViewById(R.id.bookLayout);
        mineLayout = findViewById(R.id.mineLayout);

        // 检查控件是否为 null
        if (messageLayout == null || bookLayout == null || mineLayout == null) {
            Log.d("qwe_TabActivity", "控件初始化失败");
        }

        // 设置点击事件
        messageLayout.setOnClickListener(this);
        bookLayout.setOnClickListener(this);
        mineLayout.setOnClickListener(this);
        // 初始化 Fragment
        initFragment();
    }

    //    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.messageLayout) {
            showFragment(0);
        } else if (v.getId() == R.id.bookLayout) {
            showFragment(1);
        } else if (v.getId() == R.id.mineLayout) {
            showFragment(2);
        }
    }

    private void initFragment(){
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        messageLayout.setBackgroundColor(Color.parseColor("#6495ED"));
        bookLayout.setBackgroundColor(Color.parseColor("#6495ED"));
        mineLayout.setBackgroundColor(Color.parseColor("#6495ED"));
        // 添加 Fragment 并隐藏除第一个外的所有 Fragment
        transaction.add(R.id.frame_content,messageFragment);
        transaction.add(R.id.frame_content,bookFragment);
        transaction.add(R.id.frame_content,mineFragment);
        transaction.hide(bookFragment);
        transaction.hide(mineFragment);
        transaction.commit();
    }

    private void background(View v) {
        if(v.getId() == R.id.messageLayout){
            messageLayout.setBackgroundColor(Color.parseColor("#007FFF"));
        }else if(v.getId() == R.id.bookLayout){
            bookLayout.setBackgroundColor(Color.parseColor("#007FFF"));
        }else if(v.getId() == R.id.mineLayout){
            mineLayout.setBackgroundColor(Color.parseColor("#007FFF"));
        }
    }

    private void backgroundReturn(View v) {
        if(v.getId() == R.id.messageLayout){
            messageLayout.setBackgroundColor(Color.parseColor("#6495ED"));
        }else if(v.getId() == R.id.bookLayout){
            bookLayout.setBackgroundColor(Color.parseColor("#6495ED"));
        }else if(v.getId() == R.id.mineLayout){
            mineLayout.setBackgroundColor(Color.parseColor("#6495ED"));
        }
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(messageFragment);
        transaction.hide(bookFragment);
        transaction.hide(mineFragment);
    }

    private void showFragment(int i) {
        Log.d("qwe_点击的tab: ", String.valueOf(i));

        FragmentTransaction transaction=fragmentManager.beginTransaction();
        // 隐藏所有 Fragment
        hideFragment(transaction);
        // 显示对应的 Fragment
        switch (i){
            case 0:
                transaction.show(messageFragment);
                background(messageLayout);
                backgroundReturn(bookLayout);
                backgroundReturn(mineLayout);
                Log.d("qwe: ","点击了0");
                break;
            case 1:
                transaction.show(bookFragment);
                background(bookLayout);
                backgroundReturn(messageLayout);
                backgroundReturn(mineLayout);
                Log.d("qwe: ","点击了1");
                break;
            case 2:
                transaction.show(mineFragment);
                background(mineLayout);
                backgroundReturn(messageLayout);
                backgroundReturn(bookLayout);
                Log.d("qwe: ","点击了2");
                break;
            default:
                break;
        }
        transaction.commit();
    }




}